package jdbc;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * 别名
 */
public class JDBCDemo06 {
    public static void main(String[] args) {
        /*
            查看1年级2班的人数

            SELECT c.name,COUNT(*) num_of_stu
            FROM class c,student s
            WHERE c.id=s.class_id AND c.name='1年级2班';
         */
        try (
                Connection connection = DBUtil.getConnection()
                ){
            System.out.println("数据库连接成功");
            Statement statement = connection.createStatement();

            String sql = "SELECT c.name,COUNT(*) num_of_stu\n" +
                         "FROM class c,student s\n" +
                         "WHERE c.id=s.class_id AND c.name='1年级2班';";
            ResultSet rs = statement.executeQuery(sql);
            System.out.println("开始查询");
            while (rs.next()){
                String cName = rs.getString("c.name");
                int numOfStu = rs.getInt("num_of_stu");
                System.out.println(String.join(",",cName,numOfStu+""));
            }
            System.out.println("查询完成");
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
